import sys
import io

# 设置标准输出编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

class RegisterFileCreator:
    def __init__(self, name):
        self.name = name
        self.source_file = f"{name}.c"
        self.include_file = f"{name}.h"
        self.registers = []  # 用于存储寄存器名称和地址
        self.end_guard = f"\n#endif // __{self.name.upper()}_H__\n"
        self.format_init()

    def include_file_init(self):
        # 写入头文件格式
        define_guard = f"#ifndef __{self.name.upper()}_H__\n#define __{self.name.upper()}_H__\n\n"
        struct_def = f"struct {self.name}_register {{\n"
        for reg_name, _ in self.registers:
            struct_def += f"    uint8_t {reg_name}[2];\n"
        struct_def += "};\n\n"
        with open(self.include_file, 'w', encoding='utf-8') as file:
            file.write(define_guard)
            file.write(struct_def)
            file.write(self.end_guard)

    def source_file_init(self):
        # 写入源文件格式
        with open(self.source_file, 'w', encoding='utf-8') as file:
            file.write(f"#include \"{self.include_file}\"\n\n")

    def format_init(self):
        self.include_file_init()
        self.source_file_init()

    def create_register_files(self, reg_name, reg_address):
        # 添加寄存器到列表
        self.registers.append((reg_name, reg_address))
        # 更新结构体定义
        self.update_struct_def()
        # 在源文件中添加初始化函数
        self.add_init_function()

    def update_struct_def(self):
        struct_def = f"struct {self.name}_register {{\n"
        for reg_name, _ in self.registers:
            struct_def += f"    unsigned char {reg_name}[2];\n"
        struct_def += "};\n\n"

        # 声明初始化函数
        init_func_decl = f"struct {self.name}_register init_{self.name}_register(void);\n\n"

        # 更新头文件中的结构体定义和初始化函数声明
        with open(self.include_file, 'r+', encoding='utf-8') as file:
            content = file.read()
            # 移除文件末尾的 'end_guard' 部分
            content = content.rsplit(self.end_guard, 1)[0]
            # 移除旧的 struct 定义和初始化函数声明
            content = content.split(f"struct {self.name}_register {{", 1)[0]
            content = content.split(f"struct {self.name}_register init_{self.name}_register(void);", 1)[0]
            file.seek(0)
            file.write(content)
            file.write(struct_def)
            file.write(init_func_decl)
            file.write(self.end_guard)




    def add_init_function(self):
        # 在源文件中添加初始化函数
        init_func = f"struct {self.name}_register init_{self.name}_register() {{\n"
        init_func += f"    struct {self.name}_register regs = {{\n"
        for reg_name, reg_address in self.registers:
            init_func += f"        .{reg_name} = {{0x{reg_address:02X}, 0x00}},\n"
        init_func += "    };\n"
        init_func += "    return regs;\n"
        init_func += "}\n\n"

        with open(self.source_file, 'w', encoding='utf-8') as file:
            file.write(f"#include \"{self.include_file}\"\n\n")
            file.write(init_func)

def test_main():
    creator = RegisterFileCreator("example")
    creator.create_register_files("a", 0x00)
    creator.create_register_files("b", 0x03)
    creator.create_register_files("c", 0x05)
    creator.create_register_files("d", 0x03)

if __name__ == "__main__":
    test_main()
